// 导入Vue
import Vue from 'vue'
// 导入Vue-router
import VueRouter from 'vue-router'
// 注册一下 use
Vue.use(VueRouter)

// 导入 组件 login
import login from '../views/login/login.vue'
// 导入 组件 index
import index from '../views/index/index.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import user from '@/views/index/user/user.vue'
import chart from '@/views/index/chart/chart.vue'
import enterprise from '@/views/index/enterprise/enterprise.vue'
import subject from '@/views/index/subject/subject.vue'
import question from '@/views/index/question/question.vue'
import { getToken, removeToken } from '@/utils/token.js'
import { info } from '@/api/index.js'
// 按需要导入element ui弹框
import { Message } from 'element-ui'
import store from '@/store'

// 路由重复的错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
// 白名单
const whitePaths = ['/login']

// 创建路由对象
const router = new VueRouter({
  // 路由规则
  routes: [
    // 默认页面index
    {
      path: '/',
      // path:"/",
      redirect: '/index'
    },
    // 登录
    {
      path: '/login',
      // path:"/",
      component: login
    },
    // 首页
    {
      path: '/index',
      // path:"/",
      component: index,
      children: [
        {
          path: 'chart',
          component: chart,
          meta: {
            title: '数据概览'
          }
        },
        {
          path: 'user',
          component: user,
          meta: {
            title: '用户列表'
          }
        },
        {
          path: 'enterprise',
          component: enterprise,
          meta: {
            title: '企业列表'
          }
        },
        {
          path: 'subject',
          component: subject,
          meta: {
            title: '学科列表'
          }
        },
        {
          path: 'question',
          component: question,
          meta: {
            title: '题库列表'
          }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 白名单
  if (!whitePaths.includes(to.path.toLocaleLowerCase())) {
    // token的对与错只能由后端来判断
    if (!getToken()) {
      next('/login')
      Message.warning('登录状态有误，请检查')
    } else {
      info().then(res => {
        let code = res.data.code
        // token的对与错只能由后端来判断
        // 如果未登录
        if (code === 206) {
          next('/login')
          removeToken()
          Message.warning('登录状态有误，请检查')
        } else if (code === 200) {
          let { username, avatar } = res.data.data
          /* eslint-disable */
          let userInfo = {
            username,
            userIcon: process.env.VUE_APP_URL + '/' + avatar
          }

          store.commit('setUserInfo', userInfo)
          // 有登录态跳转页面
          NProgress.start()
          next()
        }
      })
    }
  } else {
    NProgress.start()
    // 是登录页
    next()
  }
})

router.afterEach(to => {
  document.title = to.meta.title
  NProgress.done()
})
// 暴露出去
export default router
